const path = require('path')// 引入path模块
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const defaultSettings = require('./src/settings.js')
function resolve (dir) {
  return path.join(__dirname, dir)// path.join(__dirname)设置绝对路径
}

const name = defaultSettings.title || '老鼠扛刀满街找猫!'

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  // 静态资源打包路径
  assetsDir: 'static',

  devServer: {
    clientLogLevel: 'warning',
    port: 8080,
    open: true,
    overlay: {
      warnings: false,
      errors: false
    },
    proxy: {
      '/api': {
        target: process.env.VUE_APP_API_PREFIX,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },

  productionSourceMap: false,
  configureWebpack: {
    name: name
  },
  chainWebpack: (config) => {
    config.resolve.alias
    // set第一个参数：设置的别名，第二个参数：设置的路径
      .set('@', resolve('./src'))

    config.plugin('CompressionWebpackPlugin')
      .use(new CompressionWebpackPlugin({
        filename: '[path].gzip[query]', // 提示compression-webpack-plugin@2.0.0的话filename改为asset
        algorithm: 'gzip',
        test: /\.js$|\.html$|\.css/, // 匹配文件名
        threshold: 10240, // 对超过10k的数据压缩
        deleteOriginalAssets: false // 不删除源文件
        // minRatio: 0.8
      }))
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  }
}
